﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using HHDBUtility;
namespace HHCommon
{
    public class PagePower
    {
        #region //页面权限无限级递归
        StringBuilder PagePowerInfo = new StringBuilder();
        /// <summary>
        /// 获取页面权限无限级
        /// </summary>
        /// <param name="strsql">SQL语句</param>
        /// <returns></returns>
        public string GetPagePowerInfo(string strsql)
        {
            DataTable dt = DbHelperSQL.Query(strsql).Tables[0];
            DataRow[] drs = dt.Select("ParentID= " + 0);
            foreach (DataRow dr in drs)
            {
                int id = Convert.ToInt32(dr["ID"].ToString());
                string Name = "|--" + dr["Name"].ToString();
                PagePowerInfo.Append("<option value=\"" + id + "\">" + Name + "</option>");
                BindNode(id, dt, "|--", 0);
            }
            return PagePowerInfo.ToString();
        }

        /// <summary>
        /// 绑定子分类
        /// </summary>
        /// <param name="parentid">父级ID</param>
        /// <param name="dt">数据源</param>
        /// <param name="blank">特殊符"|--"</param>
        /// <param name="length">控制分类级前面空格的长度</param>
        public void BindNode(int parentid, DataTable dt, string blank, int length)
        {
            DataRow[] drs = dt.Select("parentID= " + parentid);
            length++;
            foreach (DataRow dr in drs)
            {
                string id = dr["ID"].ToString();
                string Name = dr["Name"].ToString();
                Name = AddTab(length) + blank + Name;
                PagePowerInfo.Append("<option value=\"" + id + "\">" + Name + "</option>");
                string blank2 = blank;
                BindNode(int.Parse(id), dt, blank2, length);
            }
        }

        //添加空格
        public string AddTab(int i)
        {
            string kg = "　";
            for (int j = 1; j < i; j++)
            {
                kg += "　";
            }
            return kg;
        }
        #endregion
    }
}
